//
//  PaperBasePanelView.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/15.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import Then
import SnapKit
import RxSwift
import Async

class PaperBasePanelView: UIView {
    var disposeBag = DisposeBag()
    
    var leftBtn: UIButton!
    var rightBtn: UIButton!
    var progressView: UIProgressView!
    var indexLable: UILabel!
    
    
    var reverseHandle: VoidBlock?
    var forwardHandle: VoidBlock?
    var cardHandle: VoidBlock?
    var submitHandle: VoidBlock?
    
    var total: Int? = 0
    var index: Int = 0{
        didSet {
            guard let count = total else { return }
            indexLable.text = "\(index + 1)/\(count)"
            let progress = Float(index + 1) / Float(count)
            progressView.setProgress(progress, animated: false)
            
            leftBtn.isEnabled = (index == 0) ? false : true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setup() {
        
        _ = UIView().then({ (view) in
            view.backgroundColor = UIColor.lsf_color16(0xD8D8D8)
            addSubview(view)
            view.snp.makeConstraints({ (maker) in
                maker.leading.trailing.top.equalTo(0)
                maker.height.equalTo(0.5)
            })
        })
        
        leftBtn = UIButton.init(type: .custom).then({ (btn) in
            btn.setImage(UIImage.init(named: "white_arror_left"), for: .normal)
            btn.backgroundColor = Config.default.themeColor
            btn.clipsToBounds = true
            btn.layer.cornerRadius = 18
            
            addSubview(btn)
            btn.rx.tap.bind { [weak self] in
                self?.reverseHandle?()
                
                //延迟0.25秒，防止用户快速点击，造成页面异常显示问题
                btn.isEnabled = false
                Async.main(after: 0.25, { [weak btn] in
                    btn?.isEnabled = true
                })
            }.disposed(by: disposeBag)
            
            btn.snp.makeConstraints({ (maker) in
                maker.size.equalTo(CGSize.init(width: 36, height: 36))
                maker.centerY.equalToSuperview()
                maker.leading.equalTo(11)
            })
        })
        
        rightBtn = UIButton.init(type: .custom).then({ (btn) in
            btn.setImage(UIImage.init(named: "white_arror_right"), for: .normal)
            btn.backgroundColor = Config.default.themeColor
            btn.clipsToBounds = true
            btn.layer.cornerRadius = 18
            
            addSubview(btn)
            btn.rx.tap.bind { [weak self] in
                self?.forwardHandle?()
                
                //延迟0.25秒，防止用户快速点击，造成页面异常显示问题
                btn.isEnabled = false
                Async.main(after: 0.25, { [weak btn] in
                    btn?.isEnabled = true
                })
            }.disposed(by: disposeBag)
            
            
            btn.snp.makeConstraints({ (maker) in
                maker.size.equalTo(CGSize.init(width: 36, height: 36))
                maker.centerY.equalToSuperview()
                maker.trailing.equalTo(-11)
            })
        })
        
        progressView = UIProgressView.init().then({ (progress) in
            progress.progressTintColor = Config.default.themeColor
            progress.trackTintColor = UIColor.lsf_color16(0xD8D8D8)
            progress.transform = CGAffineTransform.init(scaleX: 1, y: 2.5)
            
            addSubview(progress)
        })
        
        indexLable = UILabel.init().then({ (label) in
            label.textColor = UIColor.lsf_color16(0xD8D8D8)
            label.font = UIFont.systemFont(ofSize: 12)
            
            addSubview(label)
        })
    }
}
